package com.cheyunkeji.er.view;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;


/**
 * <p>
 * 自定义Dialog
 * </p>
 * 确定、取消双按钮，确定单按钮两种模式，点击外部可以实现关闭弹出窗
 */
public class SDialog extends AbsDialog implements OnClickListener {

    private static final String TAG = SDialog.class.getSimpleName() ;

    private Context context;

    /**
     * 标题View
     */
    private LinearLayout llTitle ;

    /**
     * 标题文字view
     */
    private TextView title;
    /**
     * 内容view
     */
    private TextView content;
    /**
     * 内容图片
     */
    private ImageView contentImg;
    /**
     * 单按钮确认键view
     */
    private TextView ok;
    /**
     * 双按钮view
     */
    private LinearLayout doubleBtn;
    /**
     * 双按钮左键view
     */
    private TextView btnLeft;
    /**
     * 双按钮右键view
     */
    private TextView btnRight;
    /**
     * 事件
     */
    private SDialogListener listener;
    /**
     * 双按钮响应
     */
    private SDialog2Listener listener2;
    /**
     * 标题id
     */
    private int titleId = 0;
    /**
     * 标题
     */
    private String strTitle = null;
    /**
     * 内容id
     */
    private int contentId = 0;
    /**
     * ok按钮内容
     */
    private String strOk = null;
    /**
     * 内容
     */
    private String strContent = null;
    /**
     * 图片
     */
    private int imgRes = 0;
    /**
     * 左按钮文字字符串的资源id
     */
    private int btnLeftId = 0;
    private boolean showTitle = true;

    private boolean clickcancel = true; // 是否点击取消

    private int tag ;



    public TextView getBtnRight() {
        return btnRight;
    }

    /**
     * 右按钮文字的字符串资源 id
     */
    private int btnRightId = 0;
    /**
     * 类型
     */
    private int type;

    public final static int TYPE_1_BTN = 1;
    public final static int TYPE_2_BTN = 2;

    /**左按钮字体颜色id*/
    private int lResId;

    /**右按钮字体颜色id*/
    private int rResId;

    public void setTag(int tag) {
        this.tag = tag;
    }

    public int getTag() {
        return tag;
    }

    public interface SDialogListener {
        void onOK();
    }

    public interface SDialog2Listener {
        /**
         * 响应点击事件
         *
         * @param l    true：左按钮 false：右按钮
         * @param view
         */
        void onClick(boolean l, View view);
    }

    /**
     * 自定义Dialog（确认按钮）
     *
     * @param context   主题id
     * @param titleId   标题id：0为使用默认标题
     * @param contentId 内容id
     * @param listener  监听事件
     */
    public SDialog(Context context, int titleId, int contentId, SDialogListener listener) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = TYPE_1_BTN;
        this.titleId = titleId;
        this.contentId = contentId;
        this.listener = listener;

    }

    public SDialog(Context context ,int titleId ,String contentStr ,SDialogListener listener){
        super(context, R.style.dialog_style_dim_3);
        this.context = context ;
        this.titleId = titleId ;
        this.type = TYPE_1_BTN;
        this.strContent = contentStr ;
        this.listener = listener;
    }

    /**
     * 自定义Dialog(两个按钮)//add wll
     *
     * @param context
     * @param titleId    标题id：0为使用默认标题
     * @param strContent 内容
     * @param listener2  监听事件
     */
    public SDialog(Context context, int titleId, String strContent, SDialog2Listener listener2, int type) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = type;
        this.titleId = titleId;
        this.strContent = strContent;
        this.listener2 = listener2;

    }

    /**
     * 自定义Dialog(两个按钮)//add wll
     *
     * @param context
     * @param showTitle  标题id：0为使用默认标题
     * @param strContent 内容
     * @param listener2  监听事件
     */
    public SDialog(Context context, int titleId, boolean showTitle, String strContent, SDialog2Listener listener2, int type) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = type;
        this.titleId = titleId;
        this.showTitle = showTitle;
        this.strContent = strContent;
        this.listener2 = listener2;

    }

    /**
     * 自定义Dialog（确认按钮）//add wll
     *
     * @param context
     * @param titleId   标题id：0为使用默认标题
     * @param contentId 内容id
     * @param listener  监听事件
     */
    public SDialog(Context context, int titleId, int contentId, SDialogListener listener, int type) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = type;
        this.titleId = titleId;
        this.contentId = contentId;
        this.listener = listener;

    }

    /**
     * 自定义Dialog（确认按钮）
     *
     * @param context
     * @param strTitle   标题内容
     * @param strContent 提示信息
     * @param strOk      按钮内容
     * @param listener
     */
    public SDialog(Context context, String strTitle, String strContent, String strOk, SDialogListener listener) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = TYPE_1_BTN;
        this.strTitle = strTitle;
        this.strContent = strContent;
        this.strOk = strOk;
        this.listener = listener;

    }

    /**
     * 自定义Dialog(两个按钮)
     *
     * @param context
     * @param titleId    标题id：0为使用默认标题
     * @param strContent 内容
     * @param listener2  监听事件
     */
    public SDialog(Context context, int titleId, String strContent, SDialog2Listener listener2) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = TYPE_2_BTN;
        this.titleId = titleId;
        this.strContent = strContent;
        this.listener2 = listener2;

    }

    /**
     * 自定义Dialog(两个按钮)
     *
     * @param context
     * @param strTitle   标题
     * @param strContent 内容
     * @param btnLeftId  左按钮id
     * @param btnRightId 右按钮id
     * @param listener2  监听事件
     * @param showTitle
     */
    public SDialog(Context context, String strTitle, String strContent, int btnLeftId, int btnRightId,
                   SDialog2Listener listener2, boolean showTitle) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = TYPE_2_BTN;
        this.strTitle = strTitle;
        this.strContent = strContent;
        this.listener2 = listener2;
        this.btnLeftId = btnLeftId;
        this.btnRightId = btnRightId;
        this.showTitle = showTitle;
    }



    /**
     *
     * 此构造方法最全
     * @param context 上下文
     * @param showTitle 是否显示Title
     * @param titleId   title字符串文字对应的id
     * @param contentId  提示框内容字符串 id
     * @param btnLeftId     左侧按钮文字id
     * @param btnRightId    右侧按钮文字id
     * @param listener2     按钮点击监听器
     */
    public SDialog(Context context, boolean showTitle , int titleId ,  int contentId,int btnLeftId ,int btnRightId,SDialog2Listener listener2){

        super(context,R.style.dialog_style_dim_3);
        this.context = context ;
        this.type = TYPE_2_BTN ;
        this.titleId = titleId ;
        this.contentId = contentId ;
        this.btnLeftId = btnLeftId ;
        this.btnRightId = btnRightId ;
        this.listener2 = listener2 ;
        this.showTitle = showTitle ;
    }


    /**
     * 自定义Dialog(两个按钮)
     *
     * @param context
     * @param strTitle   标题
     * @param strContent 内容
     * @param btnLeftId  左按钮id
     * @param btnRightId 右按钮id
     * @param listener2  监听事件
     */
    public SDialog(Context context, String strTitle, String strContent, int btnLeftId, int btnRightId,
                   SDialog2Listener listener2) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = TYPE_2_BTN;
        this.strTitle = strTitle;
        this.strContent = strContent;
        this.listener2 = listener2;
        this.btnLeftId = btnLeftId;
        this.btnRightId = btnRightId;
    }

    public SDialog(Context context, String strContent, int imgRes, int btnLeftId, int btnRightId,
                   SDialog2Listener listener2, int lResId, int rResId) {
        super(context, R.style.dialog_style_dim_3);
        this.context = context;
        this.type = TYPE_2_BTN;
        this.strContent = strContent;
        this.imgRes = imgRes;
        this.btnLeftId = btnLeftId;
        this.btnRightId = btnRightId;
        this.listener2 = listener2;

        this.lResId = lResId;
        this.rResId = rResId;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.er_t_dialog);

        Log.e(TAG, "onCreate: SDialog is created");

        findViews();
        setListener();
        fillContent();
    }

    private void findViews() {
        init();
        llTitle = (LinearLayout) findViewById(R.id.ll_title);
        title = (TextView) findViewById(R.id.dialog_title);
        content = (TextView) findViewById(R.id.dialog_content);
        contentImg = (ImageView) findViewById(R.id.dialog_img);
        ok = (TextView) findViewById(R.id.ok);
        doubleBtn = (LinearLayout) findViewById(R.id.doubleBtn);
        btnLeft = (TextView) findViewById(R.id.btnL);
        btnRight = (TextView) findViewById(R.id.btnR);
    }

    private void setListener() {
        ok.setOnClickListener(this);
        btnLeft.setOnClickListener(this);
        btnRight.setOnClickListener(this);
    }

    public void setClickCancel(boolean b) {
        clickcancel = b;
    }

    private void fillContent() {
        switch (type) {
            case TYPE_1_BTN:
                this.setCanceledOnTouchOutside(false);
                doubleBtn.setVisibility(View.GONE);
                break;
            case TYPE_2_BTN:
                ok.setVisibility(View.GONE);
                break;
            default:
                break;
        }
        if (showTitle) {
            if (0 != titleId)
                title.setText(titleId);
            if (null != strTitle) {
                title.setText(strTitle);
            }
            if( 0 == titleId && null == strTitle ) {
                llTitle.setVisibility(View.GONE);
            }
        } else {
            llTitle.setVisibility(View.GONE);
        }

        if (0 != contentId)
            content.setText(contentId);
        if (null != strContent) {
            int lenContent = strContent.length();
            if (lenContent > 50)
                content.setGravity(Gravity.LEFT);
            // 内容大于100的，字体要调小
            if (lenContent > 100) {
                float density = context.getResources().getDisplayMetrics().scaledDensity;
                float size = context.getResources().getDimension(R.dimen.t_font_size_l) / density;
                content.setTextSize(size);
            }
            content.setText(strContent);
        }
        if (0 != imgRes) {
            contentImg.setImageResource(imgRes);
        }
        if (0 != btnLeftId)
            btnLeft.setText(btnLeftId);
        if (0 != btnRightId)
            btnRight.setText(btnRightId);
        if (null != strOk) {
            ok.setText(strOk);
        }

        if (0 != lResId) {
            btnLeft.setTextColor(getContext().getResources().getColor(lResId));
        }
        if (0 != rResId) {
            btnRight.setTextColor(getContext().getResources().getColor(rResId));
        }
    }

    @Override
    public void onClick(View v) {
        if (clickcancel) {
            dismiss();
        }
        if (null != listener)
            listener.onOK();
        if (null != listener2) {
            if (R.id.btnL == v.getId()) {
                listener2.onClick(true, v );
            } else {
                listener2.onClick(false, v );
            }
        }
    }

    public void setContent(String msg) {
        if (msg != null) {
            content.setText(msg);
        }
    }

    /**
     * 为内容文本设置背景图
     */
    public void setContextImg(int resId) {
        if (0 != resId) {
            content.setBackgroundResource(resId);
        }
    }

    /**
     * 设置左右按钮的颜色
     * <p/>
     * resLId - 左边按钮色值
     * resRId - 右边按钮色值
     */
    public void setBtnsClr(int resLId, int resRId) {
        btnLeft.setTextColor(resLId);
        btnRight.setTextColor(resRId);
    }

}
